package sonnensystem;

import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author Sebastian Bauer
 */
public class Parser {

    public static double SCALEFAKTOR_UMLAUFBAHN = 1e-1; // Millionen Kilometer in 10 Mrd Meter
    public static double SCALEFAKTOR_RADIUS = 1e-4; // Km Radius in 1 Mio Meter
    private ArrayList<Himmelskoerper> planets;
    private Sheet sheet;
    private Workbook wb;
    private double sonnenOffset = 0;

    @SuppressWarnings("CallToThreadDumpStack")
    public Parser(String file) {

        try {
            planets = new ArrayList<>();
            wb = new XSSFWorkbook(file);
            sheet = wb.getSheetAt(0);
        } catch (Exception ex) {

            ex.printStackTrace();
        }
    }

    public List<Himmelskoerper> parse() {
        for (Row row : sheet) {
            if (row.getRowNum() == 4) {
                //Sonne
                Stern sonne = new Stern();
                sonne.setName(row.getCell(0).toString());
                sonnenOffset = row.getCell(2).getNumericCellValue() * SCALEFAKTOR_RADIUS;
                sonne.setRadius(sonnenOffset);
                sonne.setFarbe(row.getCell(6).toString());

                planets.add(sonne);
            } else if (row.getRowNum() > 4) {
                //Rest
                Planet planet = new Planet();
                planet.setName(row.getCell(0).toString());
                planet.setAbstand(row.getCell(1).getNumericCellValue() * SCALEFAKTOR_UMLAUFBAHN + sonnenOffset);
                planet.setRadius(row.getCell(2).getNumericCellValue() * SCALEFAKTOR_RADIUS);
                planet.setRotation(row.getCell(3).getNumericCellValue());
                planet.setUmlaufzeit(row.getCell(4).getNumericCellValue());
                
                if (row.getCell(5) != null) {
                    planet.setTexturpfad(row.getCell(5).toString());
                }
                if (row.getCell(6) != null) {
                    planet.setFarbe(row.getCell(6).toString());
                }

                planets.add(planet);
            }
        }

        return planets;
    }
}
